<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
*  author khanhdq
*  email khanhdq119@gmail.com
*/

/**
* loai bo tieng viet va cac ky tu dac biet
*/
if( ! function_exists('removeMarks'))
{
	function removeMarks($string)
	{
	  $trans 	= 	array ('"'=>'','"'=>'',' - '=>'-','!'=>'','.'=>'','&'=>'',','=>'',' & '=>'-','é' => 'e', "'" => "",  '"' => '', '"' => '', 'ẻ' => 'e', 'ẽ' => 'e', 'ằ' => 'a', 'ắ' => 'a', 'ọ' => 'o', 'ẽ' => 'e', 'ờ' => 'o', 'ẹ' => 'e', 'ặ' => 'a', 'ề' => 'e', 'ặ' => 'a', 'à' => 'a', 'á' => 'a', 'ả' => 'a', 'ã' => 'a', 'ạ' => 'a', 'â' => 'a', 'ấ' => 'a', 'ầ' => 'a', 'ẫ' => 'a', 'ẩ' => 'a', 'ậ' => 'a', 'ú' => 'a', 'ù' => 'u', 'ủ' => 'u', 'ũ' => 'u', 'ụ' => 'u', 'à' => 'a', 'á' => 'a', 'ô' => 'o', 'ố' => 'o', 'ồ' => 'o', 'ổ' => 'o', 'ỗ' => 'o', 'ộ' => 'o', 'ó' => 'o', 'ò' => 'o', 'ỏ' => 'o', 'õ' => 'o', 'ọ' => 'o', 'ê' => 'e', 'ế' => 'e', 'ề' => 'e', 'ể' => 'e', 'ễ' => 'e', 'ệ' => 'e', 'í' => 'i', 'ì' => 'i', 'ỉ' => 'i', 'ĩ' => 'i', 'ị' => 'i', 'ơ' => 'o', 'ớ' => 'o', 'ý' => 'y', 'ỳ' => 'y', 'ỷ' => 'y', 'ỹ' => 'y', 'ỵ' => 'y', 'ờ' => 'o', 'ở' => 'o', 'ỡ' => 'o', 'ợ' => 'o', 'ư' => 'u', 'ừ' => 'u', 'ứ' => 'u', 'ử' => 'u', 'ữ' => 'u', 'ự' => 'u', 'đ' => 'd', 'À' => 'A', 'Á' => 'A', 'Ả' => 'A', 'Ã' => 'A', 'Ạ' => 'A', 'Â' => 'A', 'Ấ' => 'A', 'À' => 'A', 'Ẫ' => 'A', 'Ẩ' => 'A', 'Ậ' => 'A', 'Ú' => 'U', 'Ù' => 'U', 'Ủ' => 'U', 'Ũ' => 'U', 'Ụ' => 'U', 'Ô' => 'O', 'Ố' => 'O', 'Ồ' => 'O', 'Ổ' => 'O', 'Ỗ' => 'O', 'Ộ' => 'O', 'Ê' => 'E', 'Ế' => 'E', 'Ề' => 'E', 'Ể' => 'E', 'Ễ' => 'E', 'Ệ' => 'E', 'Í' => 'I', 'Ì' => 'I', 'Ỉ' => 'I', 'Ĩ' => 'I', 'Ị' => 'I', 'Ơ' => 'O', 'Ớ' => 'O', 'Ờ' => 'O', 'Ở' => 'O', 'Ỡ' => 'O', 'Ợ' => 'O', 'Ư' => 'U', 'Ừ' => 'U', 'Ứ' => 'U', 'Ử' => 'U', 'Ữ' => 'U', 'Ự' => 'U', 'Đ' => 'D', 'Ý' => 'Y', 'Ỳ' => 'Y', 'Ỷ' => 'Y', 'Ỹ' => 'Y', 'Ỵ' => 'Y', 'á' => 'a', 'à' => 'a', 'ả' => 'a', 'ã' => 'a', 'ạ' => 'a', 'ă' => 'a', 'ắ' => 'a', 'ằ' => 'a', 'ẳ' => 'a', 'ẵ' => 'a', 'ặ' => 'a', 'â' => 'a', 'ấ' => 'a', 'ầ' => 'a', 'ẩ' => 'a', 'ẫ' => 'a', 'ậ' => 'a', 'ú' => 'u', 'ù' => 'u', 'ủ' => 'u', 'ũ' => 'u', 'ụ' => 'u', 'ư' => 'u', 'ứ' => 'u', 'ừ' => 'u', 'ử' => 'u', 'ữ' => 'u', 'ự' => 'u', 'í' => 'i', 'ì' => 'i', 'ỉ' => 'i', 'ĩ' => 'i', 'ị' => 'i', 'ó' => 'o', 'ò' => 'o', 'ỏ' => 'o', 'õ' => 'o', 'ọ' => 'o', 'ô' => 'o', 'ố' => 'o', 'ồ' => 'ô', 'ổ' => 'o', 'ỗ' => 'o', 'ộ' => 'o', 'ơ' => 'o', 'ớ' => 'o', 'ờ' => 'o', 'ở' => 'o', 'ỡ' => 'o', 'ợ' => 'o', 'đ' => 'd', 'Đ' => 'D', 'ý' => 'y', 'ỳ' => 'y', 'ỷ' => 'y', 'ỹ' => 'y', 'ỵ' => 'y', 'Á' => 'A', 'À' => 'A', 'Ả' => 'A', 'Ã' => 'A', 'Ạ' => 'A', 'Ă' => 'A', 'Ắ' => 'A', 'Ẳ' => 'A', 'Ẵ' => 'A', 'Ặ' => 'A', 'Â' => 'A', 'Ấ' => 'A', 'Ẩ' => 'A', 'Ẫ' => 'A', 'Ậ' => 'A', 'É' => 'E', 'È' => 'E', 'Ẻ' => 'E', 'Ẽ' => 'E', 'Ẹ' => 'E', 'Ế' => 'E', 'Ề' => 'E', 'Ể' => 'E', 'Ễ' => 'E', 'Ệ' => 'E', 'Ú' => 'U', 'Ù' => 'U', 'Ủ' => 'U', 'Ũ' => 'U', 'Ụ' => 'U', 'Ư' => 'U', 'Ứ' => 'U', 'Ừ' => 'U', 'Ử' => 'U', 'Ữ' => 'U', 'Ự' => 'U', 'Í' => 'I', 'Ì' => 'I', 'Ỉ' => 'I', 'Ĩ' => 'I', 'Ị' => 'I', 'Ó' => 'O', 'Ò' => 'O', 'Ỏ' => 'O', 'Õ' => 'O', 'Ọ' => 'O', 'Ô' => 'O', 'Ố' => 'O', 'Ổ' => 'O', 'Ỗ' => 'O', 'Ộ' => 'O', 'Ơ' => 'O', 'Ớ' => 'O', 'Ờ' => 'O', 'Ở' => 'O', 'Ỡ' => 'O', 'Ợ' => 'O', 'Ý' => 'Y', 'Ỳ' => 'Y', 'Ỷ' => 'Y', 'Ỹ' => 'Y', 'Ỵ' => 'Y','?'=>'', ' ' => '-', '/'=>'');
	  $string	= 	strtr(trim($string), $trans);
	  $string   =   trim(preg_replace('/[^\w\d_ -]/si', '', $string));//remove all illegal chars
	  $string 	= 	preg_replace("/^[^a-z0-9]?(.*?)[^a-z0-9]?$/i", "$1", $string);
	  return $string;
	}
}
/**
*  author khanhdq
*  email khanhdq119@gmail.com
*/
if( ! function_exists('cartMarks'))
{
    function cartMarks($string)
    {
        $trans 	= 	array ('"'=>'','/'=>'-');
        $string	= 	strtr(trim($string), $trans);
        $string   =   trim(preg_replace('/[^\w\d_ -]/si', '', $string));//remove all illegal chars
        $string 	= 	preg_replace("/^[^a-z0-9]?(.*?)[^a-z0-9]?$/i", "$1", $string);
        return $string;
    }
}
/**
* loai bo tieng viet
*/
if( ! function_exists('removeMarksTV'))
{
	function removeMarksTV($string)
	{
	  $trans 	= 	array ('é' => 'e', 'ẻ' => 'e', 'ẽ' => 'e', 'ằ' => 'a', 'ắ' => 'a', 'ọ' => 'o', 'ẽ' => 'e', 'ờ' => 'o', 'ẹ' => 'e', 'ặ' => 'a', 'ề' => 'e', 'ặ' => 'a', 'à' => 'a', 'á' => 'a', 'ả' => 'a', 'ã' => 'a', 'ạ' => 'a', 'â' => 'a', 'ấ' => 'a', 'ầ' => 'a', 'ẫ' => 'a', 'ẩ' => 'a', 'ậ' => 'a', 'ú' => 'a', 'ù' => 'u', 'ủ' => 'u', 'ũ' => 'u', 'ụ' => 'u', 'à' => 'a', 'á' => 'a', 'ô' => 'o', 'ố' => 'o', 'ồ' => 'o', 'ổ' => 'o', 'ỗ' => 'o', 'ộ' => 'o', 'ó' => 'o', 'ò' => 'o', 'ỏ' => 'o', 'õ' => 'o', 'ọ' => 'o', 'ê' => 'e', 'ế' => 'e', 'ề' => 'e', 'ể' => 'e', 'ễ' => 'e', 'ệ' => 'e', 'í' => 'i', 'ì' => 'i', 'ỉ' => 'i', 'ĩ' => 'i', 'ị' => 'i', 'ơ' => 'o', 'ớ' => 'o', 'ý' => 'y', 'ỳ' => 'y', 'ỷ' => 'y', 'ỹ' => 'y', 'ỵ' => 'y', 'ờ' => 'o', 'ở' => 'o', 'ỡ' => 'o', 'ợ' => 'o', 'ư' => 'u', 'ừ' => 'u', 'ứ' => 'u', 'ử' => 'u', 'ữ' => 'u', 'ự' => 'u', 'đ' => 'd', 'À' => 'A', 'Á' => 'A', 'Ả' => 'A', 'Ã' => 'A', 'Ạ' => 'A', 'Â' => 'A', 'Ấ' => 'A', 'À' => 'A', 'Ẫ' => 'A', 'Ẩ' => 'A', 'Ậ' => 'A', 'Ú' => 'U', 'Ù' => 'U', 'Ủ' => 'U', 'Ũ' => 'U', 'Ụ' => 'U', 'Ô' => 'O', 'Ố' => 'O', 'Ồ' => 'O', 'Ổ' => 'O', 'Ỗ' => 'O', 'Ộ' => 'O', 'Ê' => 'E', 'Ế' => 'E', 'Ề' => 'E', 'Ể' => 'E', 'Ễ' => 'E', 'Ệ' => 'E', 'Í' => 'I', 'Ì' => 'I', 'Ỉ' => 'I', 'Ĩ' => 'I', 'Ị' => 'I', 'Ơ' => 'O', 'Ớ' => 'O', 'Ờ' => 'O', 'Ở' => 'O', 'Ỡ' => 'O', 'Ợ' => 'O', 'Ư' => 'U', 'Ừ' => 'U', 'Ứ' => 'U', 'Ử' => 'U', 'Ữ' => 'U', 'Ự' => 'U', 'Đ' => 'D', 'Ý' => 'Y', 'Ỳ' => 'Y', 'Ỷ' => 'Y', 'Ỹ' => 'Y', 'Ỵ' => 'Y', 'á' => 'a', 'à' => 'a', 'ả' => 'a', 'ã' => 'a', 'ạ' => 'a', 'ă' => 'a', 'ắ' => 'a', 'ằ' => 'a', 'ẳ' => 'a', 'ẵ' => 'a', 'ặ' => 'a', 'â' => 'a', 'ấ' => 'a', 'ầ' => 'a', 'ẩ' => 'a', 'ẫ' => 'a', 'ậ' => 'a', 'ú' => 'u', 'ù' => 'u', 'ủ' => 'u', 'ũ' => 'u', 'ụ' => 'u', 'ư' => 'u', 'ứ' => 'u', 'ừ' => 'u', 'ử' => 'u', 'ữ' => 'u', 'ự' => 'u', 'í' => 'i', 'ì' => 'i', 'ỉ' => 'i', 'ĩ' => 'i', 'ị' => 'i', 'ó' => 'o', 'ò' => 'o', 'ỏ' => 'o', 'õ' => 'o', 'ọ' => 'o', 'ô' => 'o', 'ố' => 'o', 'ồ' => 'ô', 'ổ' => 'o', 'ỗ' => 'o', 'ộ' => 'o', 'ơ' => 'o', 'ớ' => 'o', 'ờ' => 'o', 'ở' => 'o', 'ỡ' => 'o', 'ợ' => 'o', 'đ' => 'd', 'Đ' => 'D', 'ý' => 'y', 'ỳ' => 'y', 'ỷ' => 'y', 'ỹ' => 'y', 'ỵ' => 'y', 'Á' => 'A', 'À' => 'A', 'Ả' => 'A', 'Ã' => 'A', 'Ạ' => 'A', 'Ă' => 'A', 'Ắ' => 'A', 'Ẳ' => 'A', 'Ẵ' => 'A', 'Ặ' => 'A', 'Â' => 'A', 'Ấ' => 'A', 'Ẩ' => 'A', 'Ẫ' => 'A', 'Ậ' => 'A', 'É' => 'E', 'È' => 'E', 'Ẻ' => 'E', 'Ẽ' => 'E', 'Ẹ' => 'E', 'Ế' => 'E', 'Ề' => 'E', 'Ể' => 'E', 'Ễ' => 'E', 'Ệ' => 'E', 'Ú' => 'U', 'Ù' => 'U', 'Ủ' => 'U', 'Ũ' => 'U', 'Ụ' => 'U', 'Ư' => 'U', 'Ứ' => 'U', 'Ừ' => 'U', 'Ử' => 'U', 'Ữ' => 'U', 'Ự' => 'U', 'Í' => 'I', 'Ì' => 'I', 'Ỉ' => 'I', 'Ĩ' => 'I', 'Ị' => 'I', 'Ó' => 'O', 'Ò' => 'O', 'Ỏ' => 'O', 'Õ' => 'O', 'Ọ' => 'O', 'Ô' => 'O', 'Ố' => 'O', 'Ổ' => 'O', 'Ỗ' => 'O', 'Ộ' => 'O', 'Ơ' => 'O', 'Ớ' => 'O', 'Ờ' => 'O', 'Ở' => 'O', 'Ỡ' => 'O', 'Ợ' => 'O', 'Ý' => 'Y', 'Ỳ' => 'Y', 'Ỷ' => 'Y', 'Ỹ' => 'Y', 'Ỵ' => 'Y');
	  $string	= 	strtr(trim($string), $trans);
	  $string   =   trim(preg_replace('/[^\w\d_ -]/si', '', $string));//remove all illegal chars
	  $string 	= 	preg_replace("/^[^a-z0-9]?(.*?)[^a-z0-9]?$/i", "$1", $string);
	  return $string;
	}
}

/**
 * Set logged in session data
 * @param $returnCode
 * @return unknown_type
 */
if( ! function_exists('setLoggedInSessionData'))
{
	function setLoggedInSessionData($account,$flag = true) {
		$CI =& get_instance();
		/*
		 * Xoa cac session luu trong ci_sessions co cung account
		 * De tranh viec 1 tai khoan dang nhap o nhieu noi cung mot luc
		 */
	    $CI->db->where(array('account_id'=>$account->id));
	    $CI->db->delete('ci_sessions');
		/*
		 * Luu session data moi
		 */
		$CI->session->set_userdata('account_id', $account->id);
		$CI->session->set_userdata('account_email', $account->Email);
		$CI->session->set_userdata('account_name', $account->Name);
		$CI->session->set_userdata('account_phone_no', $account->Phone);
		if(isset($account->gender)){
			$CI->session->set_userdata('account_gender', $account->gender);
		}
		if(isset($account->address)){
			$CI->session->set_userdata('account_address', $account->address);
		}
		if(isset($account->birthdate)){
			$CI->session->set_userdata('account_birthday', $account->birthdate);
		}
		if(isset($account->balance)){
			$CI->session->set_userdata('account_balance', $account->balance);
		}
		if(isset($account->sub_balance)){
			$CI->session->set_userdata('account_sub_balance', $account->sub_balance);
		}
		if(isset($account->user)){
			$CI->session->set_userdata('account_user', $account->user);
		}
	}
}
if( ! function_exists('freeLoggedInSessionData'))
{
	function freeLoggedInSessionData($flag = true){
		$CI =& get_instance();
		$CI->session->set_userdata(array(
			'account_id'=>''
			,'account_email'=>''
			,'account_name'=>''
			,'account_phone_no'=>''
		));
	}
}
if ( ! function_exists('cropString'))
{
    /**
     * Crop a string to given length
     * @param   mixed
     * @param   integer
     * @return  array
     */
    function cropString($str, $length, $char=" ..."){
        $str = trim($str);

    	// Nếu chuỗi cần cắt nhỏ hơn length thì return
    	$strlen	= mb_strlen($str, "UTF-8");
    	if($strlen <= $length) return $str;

        // Cắt chiều dài chuỗi tới đoạn cần lấy
    	$substr	= mb_substr($str, 0, $length, "UTF-8");
    	if(mb_substr($str, $length, 1, "UTF-8") == " ") return $substr . $char;

        // Xác định dấu " " cuối cùng trong chuỗi vừa cắt
    	$strPoint= mb_strrpos($substr, " ", "UTF-8");

    	// Return string
    	if($strPoint < $length - 20) return $substr . $char;
    	else return mb_substr($substr, 0, $strPoint, "UTF-8") . $char;
    }
}

if ( ! function_exists('generateRandomString'))
{
	/*
	 * Generate a random string with a specified length
	 */
	function generateRandomString($length = 10)
	{
		//Bỏ các ký tự dễ nhầm lẫn: 0, O, 1, L
	    $letters = '23456789QWERTYUPASDFGHJKZXCVBNM';
	    $s = '';
	    $lettersLength = strlen($letters)-1;

	    for($i = 0 ; $i < $length ; $i++)
	    {
    		$s .= $letters[rand(0,$lettersLength)];
	    }

	    return $s;
	}
}
/**
* tao check sum
*
* @param array $row
* @return string
*/
if ( ! function_exists('getChecksum'))
{
	function getChecksum($row){
		if(!$row){
			return "";
		}

		$secret_key = 'BkdKt09*@asFRe1r3o24.n$_e3xN';
		//Nếu là object thì chuyển sang thành mảng
		if (is_object($row)){
			$row = get_object_vars($row);
		}
		ksort($row);
		$data = '';
		foreach($row as $key => $value){
			if ($key !== 'checksum'){
				$data .= $value;
			}
		}
		$data .= $secret_key;
		return md5($data);
	}
}
/**
* kiem tra trang thai dang nhap
*/
if( ! function_exists('requireLogin'))
{
	function requireLogin()
	{
		$CI =& get_instance();
		if(!$CI->session->userdata('account_name')){
			$urlRecent = base64_encode($CI->uri->full_url_address());
			redirect(BASE_URL.'/accounts/login?url='.$urlRecent);
		}
	}
}

/**
*  lay ten domain
*/
if( ! function_exists('get_domain'))
{
	function get_domain($url) {
		$host = parse_url($url,PHP_URL_HOST);
		$domain = str_replace("www.",'',$host);
		return $domain;
	}
}

/**
* kiem tra domain co ton tai ko
* @param $str
*/
if( ! function_exists('check_domain'))
{
	function check_domain($url)
	{
		 $domain = get_domain($url);
  		 if(file_get_contents("http://www.pavietnam.vn/whois.php?domain=$domain") == "0"){
    	 	return TRUE;
  		 } else{
			 return FALSE;
  		 }
	}
}

// phan trang
if( ! function_exists('create_paging'))
{
	function create_paging($iPerpage, $iTotalRecord, $iCurrentPage, $sUrlPath)
	{
		require(APPPATH."ext_libraries/paging/paging.php");
		$oPaging = new paging($iPerpage, $iTotalRecord, $iCurrentPage, $sUrlPath);
		return $oPaging->getStringPaging();
	}
}
// phan tich cau hinh giao dien dai ly
if( ! function_exists('analytic_config_merchant'))
{
	function analytic_config_merchant($cfg)
	{
		$config = '';
		$ar_cfg = explode("-",$cfg);
		$config->width = $ar_cfg[0];
		$config->font = $ar_cfg[2];
		$config->color = $ar_cfg[3];
		$config->button = $ar_cfg[4];
		return $config;
	}
}
// tao 1 chuoi ma hoa
if( ! function_exists('cipher_salt'))
{
	function cipher_salt($id)
	{
		$cipher = "tx3h@%^*f3g";
		return md5($id.$cipher);
	}
}


// kiem tra chuoi ma hoa
if( ! function_exists('verify_cipher_salt'))
{
	function verify_cipher_salt($security,$id)
	{
		$cipher = "tx3h@%^*f3g";
		return md5($id.$cipher) == $security;
	}
}

function toKeyNamePairs($keyField = '', $nameField = '', $data, $emptyItemKey = '', $emptyItemName = ''){
	$keyNamePairs = array();
	if (strlen($emptyItemKey)>0){
		$keyNamePairs[$emptyItemKey] = $emptyItemName;
	}

	foreach($data as $item){
		$keyNamePairs[$item->$keyField] = $item->$nameField;
	}

	return $keyNamePairs;
}

function strToTimestamp($str = '', $delimiter = '/',$last=FALSE){
	if (!$str)
		return NULL;
	$element = explode($delimiter, $str);
	$day = $element[0];
	$month = $element[1];
	$year = $element[2];
	if(!$last)
		return mktime(0,0,0, $month, $day, $year);
	else
		return mktime(23,59,59, $month, $day, $year);
}
// END common_helper



/* End of file */
